


import 'package:bike_ride/api/login_repository.dart';
import 'package:bike_ride/common/mvvm/base_view_model.dart';
import 'package:bike_ride/model/login/view/login_v.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';

class LoginViewModel extends PageViewModel<LoginViewState> {
  CancelToken? cancelToken;

  /// 发送验证码
  final sendMobileRunning = ValueNotifier<bool>(false);
  ValueNotifier<String> appName = ValueNotifier<String>('');
  ValueNotifier<LoginType> loginType = ValueNotifier<LoginType>(LoginType.empty);

  /// 请求数据
  @override
  onCreate() {
    cancelToken = CancelToken();
    getAppName();
  }

  @override
  onDispose() {
    if (!(cancelToken?.isCancelled ?? true)) {
      cancelToken?.cancel();
    }
    super.onDispose();
  }

  Future getAppName()async{
    final info = await PackageInfo.fromPlatform();
    appName.value = info.appName;
  }

  Future<bool> mobileSendCode({required String mobile}) async {
    //方便提交苹果市场的时候验证码不会被重置
    if(mobile == '19120675562'){
      mobile = '15907977476';//不知道是谁的
    }
    return LoginRepository.mobileSendIdl(
        cancelToken: cancelToken,
        mobile: mobile
    );
  }

  Future<bool> mobileLogin({required String mobile,required String code}) async {
    return LoginRepository.mobileLoginIdl(
        cancelToken: cancelToken,
        mobile: mobile,
        code: code,
    );
  }

  Future<bool> emailSendCode({required String email}) async {
    return LoginRepository.emailSendIdl(
        cancelToken: cancelToken,
        email: email
    );
  }

  Future<bool> emailLogin({required String email,required String code}) async {
    return LoginRepository.emailLoginIdl(
      cancelToken: cancelToken,
      email: email,
      code: code,
    );
  }

  @override
  Future<PageViewModel<State<StatefulWidget>>?> requestData({Map<String, dynamic>? params}) {
    throw UnimplementedError();
  }

}